package com.example.ehCache;

import javassist.NotFoundException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/1/12 0012.
 */
@RestController
public class DemoInfo2Controller {

    @Resource
    private DemoInfo2Service demoInfo2Service;

    @RequestMapping("/ehtest")
    public String test(){

        //存入两条数据.
        DemoInfo2 demoInfo = new DemoInfo2();
        demoInfo.setName("张三");
        demoInfo.setPwd("123456");
        DemoInfo2 demoInfo2 = demoInfo2Service.save(demoInfo);

        //不走缓存.
        System.out.println("--->d1 "+ demoInfo2Service.findById(demoInfo2.getId()));
        //走缓存.
        System.out.println("--->d1 "+ demoInfo2Service.findById(demoInfo2.getId()));


        demoInfo = new DemoInfo2();
        demoInfo.setName("李四");
        demoInfo.setPwd("123456");
        DemoInfo2 demoInfo3 = demoInfo2Service.save(demoInfo);

        //不走缓存---? 走了缓存
        System.out.println("--->d2 "+ demoInfo2Service.findById(demoInfo3.getId()));
        //走缓存.
        System.out.println("--->d2 "+ demoInfo2Service.findById(demoInfo3.getId()));

        System.out.println("============修改数据=====================");
        //修改数据.
        DemoInfo2 updated = new DemoInfo2();
        updated.setName("李四-updated");
        updated.setPwd("123456");
        updated.setId(demoInfo3.getId());
        try {
            System.out.println("--->du1 "+ demoInfo2Service.update(updated));//没走缓存
        } catch (NotFoundException e) {
            e.printStackTrace();
        }

        //不走缓存.--->走缓存
        System.out.println("--->du2 "+ demoInfo2Service.findById(updated.getId()));

        return "ok";
    }
}
